#include <QApplication>
#include "ElaApplication.h"
#include "ElaTheme.h"
#include "ElaWidget.h"
#include "ElaSlider.h"
#include "ElaText.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QCheckBox>
#include <QSpinBox>
#include <QDebug>

class ElaSliderTest : public ElaWidget
{
    Q_OBJECT

public:
    ElaSliderTest(QWidget *parent = nullptr) : ElaWidget(parent)
    {
        setWindowTitle("ElaSlider 功能测试");
        resize(800, 700);
        setWindowButtonFlags(ElaAppBarType::StayTopButtonHint | ElaAppBarType::MinimizeButtonHint | 
                           ElaAppBarType::MaximizeButtonHint | ElaAppBarType::CloseButtonHint | 
                           ElaAppBarType::ThemeChangeButtonHint | ElaAppBarType::NavigationButtonHint);

        setupUI();
        setupConnections();
    }

private:
    void setupUI()
    {
        QVBoxLayout *mainLayout = new QVBoxLayout(this);

        // 1. 基础滑动条展示
        mainLayout->addWidget(createBasicSliderGroup());

        // 2. 方向演示
        mainLayout->addWidget(createOrientationGroup());

        // 3. 范围与刻度
        mainLayout->addWidget(createRangeTickGroup());

        // 4. 状态演示
        mainLayout->addWidget(createStateDemoGroup());

        // 5. 应用场景演示
        mainLayout->addWidget(createApplicationGroup());

        // 6. 动态操作演示
        mainLayout->addWidget(createDynamicOperationGroup());

        mainLayout->addStretch();
    }

    // 创建基础滑动条组
    QGroupBox *createBasicSliderGroup()
    {
        QGroupBox *group = new QGroupBox("基础滑动条功能", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        ElaText *infoLabel = new ElaText("展示基本的滑动条功能和数值变化", this);
        infoLabel->setTextStyle(ElaTextType::Body);
        layout->addWidget(infoLabel);

        // 默认滑动条
        QHBoxLayout *defaultLayout = new QHBoxLayout();
        ElaText *defaultLabel = new ElaText("默认滑动条 (0-100):", this);
        defaultLabel->setTextStyle(ElaTextType::BodyStrong);
        m_defaultSlider = new ElaSlider(this);
        m_defaultValueDisplay = new ElaText("50", this);
        m_defaultValueDisplay->setTextStyle(ElaTextType::Body);
        m_defaultValueDisplay->setFixedWidth(40);

        defaultLayout->addWidget(defaultLabel);
        defaultLayout->addWidget(m_defaultSlider);
        defaultLayout->addWidget(m_defaultValueDisplay);

        // 自定义范围滑动条
        QHBoxLayout *customRangeLayout = new QHBoxLayout();
        ElaText *customLabel = new ElaText("自定义范围 (0-255):", this);
        customLabel->setTextStyle(ElaTextType::BodyStrong);
        m_customRangeSlider = new ElaSlider(this);
        m_customRangeSlider->setRange(0, 255);
        m_customRangeSlider->setValue(128);
        m_customRangeValue = new ElaText("128", this);
        m_customRangeValue->setTextStyle(ElaTextType::Body);
        m_customRangeValue->setFixedWidth(40);

        customRangeLayout->addWidget(customLabel);
        customRangeLayout->addWidget(m_customRangeSlider);
        customRangeLayout->addWidget(m_customRangeValue);

        layout->addLayout(defaultLayout);
        layout->addLayout(customRangeLayout);

        return group;
    }

    // 创建方向演示组
    QGroupBox *createOrientationGroup()
    {
        QGroupBox *group = new QGroupBox("方向演示", this);
        QHBoxLayout *layout = new QHBoxLayout(group);

        // 水平滑动条
        QVBoxLayout *horizontalLayout = new QVBoxLayout();
        ElaText *horizontalLabel = new ElaText("水平滑动条", this);
        horizontalLabel->setTextStyle(ElaTextType::Subtitle);
        horizontalLabel->setAlignment(Qt::AlignCenter);
        
        m_horizontalSlider = new ElaSlider(Qt::Horizontal, this);
        m_horizontalSlider->setRange(0, 100);
        m_horizontalSlider->setValue(75);
        m_horizontalValue = new ElaText("75", this);
        m_horizontalValue->setTextStyle(ElaTextType::Body);
        m_horizontalValue->setAlignment(Qt::AlignCenter);

        horizontalLayout->addWidget(horizontalLabel);
        horizontalLayout->addWidget(m_horizontalSlider);
        horizontalLayout->addWidget(m_horizontalValue);

        // 垂直滑动条
        QVBoxLayout *verticalLayout = new QVBoxLayout();
        ElaText *verticalLabel = new ElaText("垂直滑动条", this);
        verticalLabel->setTextStyle(ElaTextType::Subtitle);
        verticalLabel->setAlignment(Qt::AlignCenter);
        
        m_verticalSlider = new ElaSlider(Qt::Vertical, this);
        m_verticalSlider->setRange(0, 100);
        m_verticalSlider->setValue(25);
        m_verticalSlider->setFixedHeight(120);
        m_verticalValue = new ElaText("25", this);
        m_verticalValue->setTextStyle(ElaTextType::Body);
        m_verticalValue->setAlignment(Qt::AlignCenter);

        verticalLayout->addWidget(verticalLabel);
        verticalLayout->addWidget(m_verticalSlider);
        verticalLayout->addWidget(m_verticalValue);

        layout->addLayout(horizontalLayout);
        layout->addLayout(verticalLayout);

        return group;
    }

    // 创建范围与刻度组
    QGroupBox *createRangeTickGroup()
    {
        QGroupBox *group = new QGroupBox("范围与刻度", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        ElaText *infoLabel = new ElaText("展示不同范围和刻度设置的滑动条", this);
        infoLabel->setTextStyle(ElaTextType::Body);
        layout->addWidget(infoLabel);

        // 单步长设置
        QHBoxLayout *singleStepLayout = new QHBoxLayout();
        ElaText *stepLabel = new ElaText("单步长 (10):", this);
        stepLabel->setTextStyle(ElaTextType::BodyStrong);
        m_singleStepSlider = new ElaSlider(this);
        m_singleStepSlider->setRange(0, 100);
        m_singleStepSlider->setSingleStep(10);
        m_singleStepSlider->setPageStep(20);
        m_singleStepSlider->setValue(50);
        m_singleStepValue = new ElaText("50", this);
        m_singleStepValue->setTextStyle(ElaTextType::Body);
        m_singleStepValue->setFixedWidth(40);

        singleStepLayout->addWidget(stepLabel);
        singleStepLayout->addWidget(m_singleStepSlider);
        singleStepLayout->addWidget(m_singleStepValue);

        // 刻度显示
        QHBoxLayout *tickLayout = new QHBoxLayout();
        ElaText *tickLabel = new ElaText("刻度显示:", this);
        tickLabel->setTextStyle(ElaTextType::BodyStrong);
        m_tickSlider = new ElaSlider(this);
        m_tickSlider->setRange(0, 50);
        m_tickSlider->setTickPosition(QSlider::TicksBelow);
        m_tickSlider->setTickInterval(10);
        m_tickSlider->setValue(25);
        m_tickValue = new ElaText("25", this);
        m_tickValue->setTextStyle(ElaTextType::Body);
        m_tickValue->setFixedWidth(40);

        tickLayout->addWidget(tickLabel);
        tickLayout->addWidget(m_tickSlider);
        tickLayout->addWidget(m_tickValue);

        layout->addLayout(singleStepLayout);
        layout->addLayout(tickLayout);

        return group;
    }

    // 创建状态演示组
    QGroupBox *createStateDemoGroup()
    {
        QGroupBox *group = new QGroupBox("状态演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        ElaText *infoLabel = new ElaText("展示滑动条在不同状态下的表现", this);
        infoLabel->setTextStyle(ElaTextType::Body);
        layout->addWidget(infoLabel);

        // 正常状态
        QHBoxLayout *normalLayout = new QHBoxLayout();
        ElaText *normalLabel = new ElaText("正常状态:", this);
        normalLabel->setTextStyle(ElaTextType::BodyStrong);
        m_normalSlider = new ElaSlider(this);
        m_normalSlider->setRange(0, 100);
        m_normalSlider->setValue(60);

        normalLayout->addWidget(normalLabel);
        normalLayout->addWidget(m_normalSlider);
        normalLayout->addStretch();

        // 禁用状态
        QHBoxLayout *disabledLayout = new QHBoxLayout();
        ElaText *disabledLabel = new ElaText("禁用状态:", this);
        disabledLabel->setTextStyle(ElaTextType::BodyStrong);
        m_disabledSlider = new ElaSlider(this);
        m_disabledSlider->setRange(0, 100);
        m_disabledSlider->setValue(30);
        m_disabledSlider->setEnabled(false);

        disabledLayout->addWidget(disabledLabel);
        disabledLayout->addWidget(m_disabledSlider);
        disabledLayout->addStretch();

        // 只读状态（通过信号阻断模拟）
        QHBoxLayout *readOnlyLayout = new QHBoxLayout();
        ElaText *readOnlyLabel = new ElaText("只读状态:", this);
        readOnlyLabel->setTextStyle(ElaTextType::BodyStrong);
        m_readOnlySlider = new ElaSlider(this);
        m_readOnlySlider->setRange(0, 100);
        m_readOnlySlider->setValue(80);

        readOnlyLayout->addWidget(readOnlyLabel);
        readOnlyLayout->addWidget(m_readOnlySlider);
        readOnlyLayout->addStretch();

        layout->addLayout(normalLayout);
        layout->addLayout(disabledLayout);
        layout->addLayout(readOnlyLayout);

        return group;
    }

    // 创建应用场景演示组
    QGroupBox *createApplicationGroup()
    {
        QGroupBox *group = new QGroupBox("应用场景演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        ElaText *infoLabel = new ElaText("展示滑动条在实际应用场景中的使用", this);
        infoLabel->setTextStyle(ElaTextType::Body);
        layout->addWidget(infoLabel);

        // 音量控制
        QGroupBox *volumeGroup = new QGroupBox("音量控制", this);
        QVBoxLayout *volumeLayout = new QVBoxLayout(volumeGroup);

        QHBoxLayout *volumeControlLayout = new QHBoxLayout();
        ElaText *volumeLabel = new ElaText("音量:", this);
        volumeLabel->setTextStyle(ElaTextType::BodyStrong);
        m_volumeSlider = new ElaSlider(this);
        m_volumeSlider->setRange(0, 100);
        m_volumeSlider->setValue(75);
        m_volumeValue = new ElaText("75%", this);
        m_volumeValue->setTextStyle(ElaTextType::Body);
        m_volumeValue->setFixedWidth(50);

        volumeControlLayout->addWidget(volumeLabel);
        volumeControlLayout->addWidget(m_volumeSlider);
        volumeControlLayout->addWidget(m_volumeValue);

        volumeLayout->addLayout(volumeControlLayout);

        // 亮度调节
        QGroupBox *brightnessGroup = new QGroupBox("亮度调节", this);
        QVBoxLayout *brightnessLayout = new QVBoxLayout(brightnessGroup);

        QHBoxLayout *brightnessControlLayout = new QHBoxLayout();
        ElaText *brightnessLabel = new ElaText("亮度:", this);
        brightnessLabel->setTextStyle(ElaTextType::BodyStrong);
        m_brightnessSlider = new ElaSlider(this);
        m_brightnessSlider->setRange(0, 100);
        m_brightnessSlider->setValue(50);
        m_brightnessValue = new ElaText("50%", this);
        m_brightnessValue->setTextStyle(ElaTextType::Body);
        m_brightnessValue->setFixedWidth(50);

        brightnessControlLayout->addWidget(brightnessLabel);
        brightnessControlLayout->addWidget(m_brightnessSlider);
        brightnessControlLayout->addWidget(m_brightnessValue);

        brightnessLayout->addLayout(brightnessControlLayout);

        // 进度控制
        QGroupBox *progressGroup = new QGroupBox("进度控制", this);
        QVBoxLayout *progressLayout = new QVBoxLayout(progressGroup);

        QHBoxLayout *progressControlLayout = new QHBoxLayout();
        ElaText *progressLabel = new ElaText("播放进度:", this);
        progressLabel->setTextStyle(ElaTextType::BodyStrong);
        m_progressSlider = new ElaSlider(this);
        m_progressSlider->setRange(0, 300); // 5分钟，以秒为单位
        m_progressSlider->setValue(90);
        m_progressValue = new ElaText("01:30", this);
        m_progressValue->setTextStyle(ElaTextType::Body);
        m_progressValue->setFixedWidth(50);

        progressControlLayout->addWidget(progressLabel);
        progressControlLayout->addWidget(m_progressSlider);
        progressControlLayout->addWidget(m_progressValue);

        progressLayout->addLayout(progressControlLayout);

        layout->addWidget(volumeGroup);
        layout->addWidget(brightnessGroup);
        layout->addWidget(progressGroup);

        return group;
    }

    // 创建动态操作演示组
    // 创建动态操作演示组
    QGroupBox *createDynamicOperationGroup()
    {
        QGroupBox *group = new QGroupBox("动态操作演示", this);
        QVBoxLayout *layout = new QVBoxLayout(group);

        ElaText *infoLabel = new ElaText("演示滑动条的动态操作和控制", this);
        infoLabel->setTextStyle(ElaTextType::Body);
        layout->addWidget(infoLabel);

        // 动态控制滑动条
        QHBoxLayout *dynamicControlLayout = new QHBoxLayout();
        ElaText *dynamicLabel = new ElaText("动态控制:", this);
        dynamicLabel->setTextStyle(ElaTextType::BodyStrong);
        m_dynamicSlider = new ElaSlider(this);
        m_dynamicSlider->setRange(0, 200);
        m_dynamicSlider->setValue(100);
        m_dynamicValue = new ElaText("100", this);
        m_dynamicValue->setTextStyle(ElaTextType::Body);
        m_dynamicValue->setFixedWidth(50);

        // 添加状态显示标签
        m_dynamicStateLabel = new ElaText("启用状态", this);
        m_dynamicStateLabel->setTextStyle(ElaTextType::Body);
        m_dynamicStateLabel->setFixedWidth(80);
        updateDynamicStateLabel();  // 初始化状态显示

        dynamicControlLayout->addWidget(dynamicLabel);
        dynamicControlLayout->addWidget(m_dynamicSlider);
        dynamicControlLayout->addWidget(m_dynamicValue);
        dynamicControlLayout->addWidget(m_dynamicStateLabel);

        // 控制按钮
        QHBoxLayout *buttonLayout = new QHBoxLayout();
        ElaText *buttonLabel = new ElaText("控制按钮:", this);
        buttonLabel->setTextStyle(ElaTextType::BodyStrong);
        QPushButton *setMinBtn = new QPushButton("设为最小值", this);
        QPushButton *setMaxBtn = new QPushButton("设为最大值", this);
        QPushButton *setMidBtn = new QPushButton("设为中间值", this);
        QPushButton *toggleEnableBtn = new QPushButton("切换启用状态", this);

        buttonLayout->addWidget(buttonLabel);
        buttonLayout->addWidget(setMinBtn);
        buttonLayout->addWidget(setMaxBtn);
        buttonLayout->addWidget(setMidBtn);
        buttonLayout->addWidget(toggleEnableBtn);
        buttonLayout->addStretch();

        // 数值输入控制
        QHBoxLayout *inputLayout = new QHBoxLayout();
        ElaText *inputLabel = new ElaText("数值输入:", this);
        inputLabel->setTextStyle(ElaTextType::BodyStrong);
        QSpinBox *valueSpinBox = new QSpinBox(this);
        valueSpinBox->setRange(0, 200);
        valueSpinBox->setValue(100);
        QPushButton *applyValueBtn = new QPushButton("应用数值", this);

        inputLayout->addWidget(inputLabel);
        inputLayout->addWidget(valueSpinBox);
        inputLayout->addWidget(applyValueBtn);
        inputLayout->addStretch();

        layout->addLayout(dynamicControlLayout);
        layout->addLayout(buttonLayout);
        layout->addLayout(inputLayout);

        m_operationButtons << setMinBtn << setMaxBtn << setMidBtn << toggleEnableBtn << applyValueBtn;
        m_valueSpinBox = valueSpinBox;

        return group;
    }

    void setupConnections()
    {
        // 基础滑动条信号连接
        connect(m_defaultSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_defaultValueDisplay->setText(QString::number(value));
            qDebug() << "默认滑动条值:" << value;
        });

        connect(m_customRangeSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_customRangeValue->setText(QString::number(value));
            qDebug() << "自定义范围滑动条值:" << value;
        });

        // 方向滑动条信号连接
        connect(m_horizontalSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_horizontalValue->setText(QString::number(value));
            qDebug() << "水平滑动条值:" << value;
        });

        connect(m_verticalSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_verticalValue->setText(QString::number(value));
            qDebug() << "垂直滑动条值:" << value;
        });

        // 范围与刻度滑动条信号连接
        connect(m_singleStepSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_singleStepValue->setText(QString::number(value));
            qDebug() << "单步长滑动条值:" << value;
        });

        connect(m_tickSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_tickValue->setText(QString::number(value));
            qDebug() << "刻度滑动条值:" << value;
        });

        // 应用场景信号连接
        connect(m_volumeSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_volumeValue->setText(QString::number(value) + "%");
            qDebug() << "音量设置:" << value << "%";
        });

        connect(m_brightnessSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_brightnessValue->setText(QString::number(value) + "%");
            qDebug() << "亮度设置:" << value << "%";
        });

        connect(m_progressSlider, &ElaSlider::valueChanged, this, [this](int value) {
            int minutes = value / 60;
            int seconds = value % 60;
            QString timeText = QString("%1:%2").arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
            m_progressValue->setText(timeText);
            qDebug() << "播放进度:" << timeText << "(" << value << "秒)";
        });

        // 只读滑动条信号阻断
        connect(m_readOnlySlider, &ElaSlider::sliderMoved, this, [this](int value) {
            // 阻断移动操作，恢复原值
            m_readOnlySlider->setValue(80);
            qDebug() << "只读滑动条: 尝试修改值被阻止，保持原值 80";
        });

        connect(m_readOnlySlider, &ElaSlider::valueChanged, this, [this](int value) {
            // 阻断值改变操作，恢复原值
            if (value != 80) {
                m_readOnlySlider->setValue(80);
                qDebug() << "只读滑动条: 值改变被阻止，恢复为 80";
            }
        });

        // 动态操作按钮连接
        // 设为最小值
        connect(m_operationButtons[0], &QPushButton::clicked, this, [this]() {
            m_dynamicSlider->setValue(m_dynamicSlider->minimum());
            qDebug() << "动态滑动条设为最小值:" << m_dynamicSlider->minimum();
        });

        // 设为最大值
        connect(m_operationButtons[1], &QPushButton::clicked, this, [this]() {
            m_dynamicSlider->setValue(m_dynamicSlider->maximum());
            qDebug() << "动态滑动条设为最大值:" << m_dynamicSlider->maximum();
        });

        // 设为中间值
        connect(m_operationButtons[2], &QPushButton::clicked, this, [this]() {
            int midValue = (m_dynamicSlider->minimum() + m_dynamicSlider->maximum()) / 2;
            m_dynamicSlider->setValue(midValue);
            qDebug() << "动态滑动条设为中间值:" << midValue;
        });

        // 切换启用状态
        // 切换启用状态
        connect(m_operationButtons[3], &QPushButton::clicked, this, [this]() {
            bool currentState = m_dynamicSlider->isEnabled();
            m_dynamicSlider->setEnabled(!currentState);
            updateDynamicStateLabel();  // 更新状态显示
            QString stateText = !currentState ? "已启用" : "已禁用";
            qDebug() << "动态滑动条" << stateText;
        });

        // 应用数值
        connect(m_operationButtons[4], &QPushButton::clicked, this, [this]() {
            int value = m_valueSpinBox->value();
            m_dynamicSlider->setValue(value);
            qDebug() << "通过输入框设置滑动条值:" << value;
        });

        // 动态滑动条值变化
        connect(m_dynamicSlider, &ElaSlider::valueChanged, this, [this](int value) {
            m_dynamicValue->setText(QString::number(value));
            m_valueSpinBox->setValue(value);
            qDebug() << "动态滑动条值变化:" << value;
        });

        // 输入框值变化同步到滑动条
        connect(m_valueSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int value) {
            if (value != m_dynamicSlider->value()) {
                m_dynamicSlider->setValue(value);
            }
        });

        // 滑动条范围变化信号
        connect(m_dynamicSlider, &ElaSlider::rangeChanged, this, [this](int min, int max) {
            qDebug() << "动态滑动条范围变化: min=" << min << ", max=" << max;
        });


    }

    // 更新动态滑动条状态显示
    void updateDynamicStateLabel()
    {
        if (m_dynamicSlider && m_dynamicStateLabel)
        {
            if (m_dynamicSlider->isEnabled())
            {
                m_dynamicStateLabel->setText("启用状态");
                m_dynamicStateLabel->setStyleSheet("color: green;");  // 绿色表示启用
            }
            else
            {
                m_dynamicStateLabel->setText("禁用状态");
                m_dynamicStateLabel->setStyleSheet("color: red;");  // 红色表示禁用
            }
        }
    }

private:
    // 基础滑动条
    ElaSlider *m_defaultSlider;
    ElaText *m_defaultValueDisplay;
    ElaSlider *m_customRangeSlider;
    ElaText *m_customRangeValue;

    // 方向演示
    ElaSlider *m_horizontalSlider;
    ElaText *m_horizontalValue;
    ElaSlider *m_verticalSlider;
    ElaText *m_verticalValue;

    // 范围与刻度
    ElaSlider *m_singleStepSlider;
    ElaText *m_singleStepValue;
    ElaSlider *m_tickSlider;
    ElaText *m_tickValue;

    // 状态演示
    ElaSlider *m_normalSlider;
    ElaSlider *m_disabledSlider;
    ElaSlider *m_readOnlySlider;

    // 应用场景
    ElaSlider *m_volumeSlider;
    ElaText *m_volumeValue;
    ElaSlider *m_brightnessSlider;
    ElaText *m_brightnessValue;
    ElaSlider *m_progressSlider;
    ElaText *m_progressValue;

    // 动态操作
    ElaSlider *m_dynamicSlider;
    ElaText *m_dynamicValue;
    ElaText *m_dynamicStateLabel;  // 新增：状态显示标签
    QList<QPushButton *> m_operationButtons;
    QSpinBox *m_valueSpinBox;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#else
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif

    QApplication app(argc, argv);
    eApp->init();

    ElaSliderTest test;
    test.moveToCenter();
    test.show();

    return app.exec();
}

#include "ElaSliderTest.moc"